package com.shwread.android.qysw.activity;

import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.ciko.code.base.BaseActicity;
import com.shwread.android.qysw10000241.R;
import com.ui.pickerview.view.TimePickerView;

import java.util.Calendar;

import logic.util.Utils;

/**
 * 入党申请
 */
public class JHBActivity extends BaseActicity implements View.OnClickListener, TimePickerView.OnTimeSelectListener {

    private TextView tvSelectSex;
    private TextView tvSubmitTime;
    private TextView tvTalkTime;
    private TextView tvBirthdayTime;
    private Button btnAskParty;
    private TimePickerView timePickerView;

    private int tag = -1;
    private static final int SUBMIT_TAG = 935;
    private static final int TALK_TAG = 519;
    private static final int BIRTHDAY_TAG = 702;

    @Override
    protected void initTopBar() {
        String titleStr = getIntent().getStringExtra("title");
        setTitle(titleStr);
        enableNavLeftImg();
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_jhb);
    }

    @Override
    protected void initView() {
        btnAskParty = (Button) findViewById(R.id.btn_ask_party);
        tvSelectSex = (TextView) findViewById(R.id.tv_select_sex);
        tvBirthdayTime = (TextView) findViewById(R.id.tv_birthday_time);
        tvSubmitTime = (TextView) findViewById(R.id.tv_submit_time);
        tvTalkTime = (TextView) findViewById(R.id.tv_talk_time);
        initTimePicker();
    }

    @Override
    protected void initAdapter() {

    }

    @Override
    protected void initDates() {
    }

    @Override
    protected void initListener() {
        btnAskParty.setOnClickListener(this);
        tvSelectSex.setOnClickListener(this);
        tvBirthdayTime.setOnClickListener(this);
        tvSubmitTime.setOnClickListener(this);
        tvTalkTime.setOnClickListener(this);
        timePickerView.setOnTimeSelectListener(this);
    }

    private void initTimePicker() {
        //时间选择器初始化及配置
        timePickerView = new TimePickerView(this, TimePickerView.Type.YEAR_MONTH_DAY);
        Calendar calendar = Calendar.getInstance();
        timePickerView.setRange(calendar.get(Calendar.YEAR) - 100, calendar.get(Calendar.YEAR) + 5);
        timePickerView.setTime(null);
        timePickerView.setCyclic(false);
        timePickerView.setCancelable(true);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.btn_ask_party:
                Utils.showToast(this, "提交成功");
                finish();
                break;
            case R.id.tv_select_sex:
                showDropPop(tvSelectSex);
                break;
            case R.id.tv_birthday_time:
                tag = BIRTHDAY_TAG;
                timePickerView.show();
                break;
            case R.id.tv_submit_time:
                tag = SUBMIT_TAG;
                timePickerView.show();
                break;
            case R.id.tv_talk_time:
                tag = TALK_TAG;
                timePickerView.show();
                break;
        }
    }

    @Override
    public void onTimeSelect(String date) {
        switch (tag) {
            case BIRTHDAY_TAG:
                tvBirthdayTime.setText(date);
                break;
            case SUBMIT_TAG:
                tvSubmitTime.setText(date);
                break;
            case TALK_TAG:
                tvTalkTime.setText(date);
                break;
            default:
                break;
        }
    }

    public void click(String sex) {//弹窗点击事件监听
        tvSelectSex.setText(sex);
    }

    private PopupWindow moreAccountPup;
    private String[] sexs = new String[]{"男", "女"};

    void showDropPop(View v) {
        View view = LayoutInflater.from(this).inflate(R.layout.login_change_account_dialog, null);
        ListView changeListView = (ListView) view.findViewById(R.id.login_change_account_listview);
        changeListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                click(sexs[position]);
                moreAccountPup.dismiss();
            }
        });
        changeListView.setFocusable(true);
        changeListView.setFocusableInTouchMode(true);
        changeListView.setCacheColorHint(Color.TRANSPARENT);
        changeListView.setDivider(new ColorDrawable(Color.GRAY));
        changeListView.setDividerHeight(1);
        SubItemAdapter mAdapter = new SubItemAdapter();
        changeListView.setAdapter(mAdapter);
        moreAccountPup = new PopupWindow(view, v.getWidth(), ViewGroup.LayoutParams.WRAP_CONTENT);
        moreAccountPup.setBackgroundDrawable(new BitmapDrawable());
        moreAccountPup.setFocusable(true);
        moreAccountPup.showAsDropDown(v, 0, 20);
    }

    private class SubItemAdapter extends BaseAdapter {
        private SubItemAdapter() {
        }

        @Override
        public int getCount() {
            return sexs.length;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            DeviceViewHolder viewHolder;
            if (convertView == null) {
                viewHolder = new DeviceViewHolder();
                convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.login_change_account_item, parent, false);
                viewHolder.name = (TextView) convertView.findViewById(R.id.login_change_account_tv);
                viewHolder.delete = (ImageView) convertView.findViewById(R.id.login_change_account_delete_tv);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (DeviceViewHolder) convertView.getTag();
            }
            viewHolder.name.setText(sexs[position]);
            viewHolder.name.setPadding(10, 10, 10, 10);
            viewHolder.delete.setVisibility(View.GONE);
            convertView.setBackgroundColor(Color.TRANSPARENT);
            return convertView;
        }

        class DeviceViewHolder {
            public TextView name;
            public ImageView delete;
        }
    }
}
